var postsData = require('../../../data/posts-data.js')
var app = getApp()
const backgroundAudioManager = wx.getBackgroundAudioManager()
// pages/posts/post-detail/post-detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collected: false,
    isPlayMusic: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let postId = options.id
    this.data.currentPostId = postId
    let postData = postsData.postList[postId]
    // this.data.postData = postData
    //如果在onLoad方法中，不是异步的去执行一个数据绑定
    //则不需要使用this.setData方法
    //只需要对this.data赋值即可实现数据绑定
    this.setData(postData)
    let postsCollected = wx.getStorageSync('posts_collected')
    if (postsCollected) {
      let collected = postsCollected[postId]
      this.setData({
        collected
      })
    } else {
      let postsCollected = {}
      postsCollected[postId] = false
      wx.setStorageSync('posts_collected', postsCollected)
    }
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === postId) {
      this.setData({
        isPlayMusic: true
      })
    }
  },
  getPostsCollectedSys() {
    let postsCollected = wx.getStorageSync('posts_collected')
    let postCollected = postsCollected[this.data.currentPostId]
    postCollected = !postCollected
    postsCollected[this.data.currentPostId] = postCollected
    wx.setStorageSync('posts_collected', postsCollected)
    this.setData({
      collected: postCollected
    })
    wx.showToast({
      title: postCollected ? '收藏成功' : '取消收藏',
      duration: 600,
      icon: 'success',
      success() {
        // console.log('suc')
      },
      fail() {
        // console.log('fail')
      },
      complete() {
        // console.log('complete')
      }
    })
  },
  getPostsColletedAsy() {
    let that = this
    wx.getStorage({
      key: "posts_collected",
      success(res) {
        let postsCollected = res.data
        let postCollected = postsCollected[that.data.currentPostId]
        postCollected = !postCollected
        postsCollected[that.data.currentPostId] = postCollected
        that.setData({
          collected: postCollected
        })
        wx.showToast({
          title: postCollected ? '收藏成功' : '取消收藏',
          duration: 600,
          icon: 'success',
          success() {
            // console.log('suc')
          },
          fail() {
            // console.log('fail')
          },
          complete() {
            // console.log('complete')
          }
        })
      }
    })
  },
  onCollectionTap(event) {
    this.getPostsCollectedSys()
    // wx.showModal({
    //   title: '提示',
    //   content: '这是一个模态弹窗',
    //   success (res) {
    //     if (res.confirm) {
    //       console.log('用户点击确定')
    //     } else if (res.cancel) {
    //       console.log('用户点击取消')
    //     }
    //   },
    //   showCancel: "true",
    //   cancelText: "不收藏",
    //   cancelColor: "#333",
    //   confirmText: "收藏",
    //   confirmColor: "#405f80"
    // })
  },
  onShareTap(event) {
    let itemList = ['分享给微信好友', '分享到朋友圈', '分享到QQ', '分享到微博']
    wx.showActionSheet({
      itemList,
      success(res) {
        wx.showModal({
          title: "用户 " + itemList[res.tapIndex],
          content: "用户是否取消?" + res.cancel + "现在无法实现分享功能，什么时候能支持呢"
        })
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  onMusicTap(event) {
    let currentPostId = this.data.currentPostId
    let musicInfo = postsData.postList[currentPostId].music
    backgroundAudioManager.title = musicInfo.title
    backgroundAudioManager.epname = musicInfo.title
    backgroundAudioManager.coverImgUrl = musicInfo.coverImg
    // 设置了 src 之后会自动播放
    backgroundAudioManager.src = musicInfo.url
    //若要绑定到wxml文件上，那么this.data.isPlayMusic不能改变值，只能通过this.setData()改变会刷新页面绑定（js对象变了，页面也要相应刷新的意思）
    //若不需要绑定到wxml，只是在js内部使用，那么this.data.isPlayMusic可以改变值（js对象改变了，页面没有刷新绑定，页面也没有用到这个属性，因为这个属性不是绑定属性）
    let isPlayMusic = !this.data.isPlayMusic
    app.globalData.g_isPlayingMusic = isPlayMusic
    if (isPlayMusic) {
      backgroundAudioManager.play()
      app.globalData.g_currentMusicPostId = currentPostId
    } else {
      backgroundAudioManager.pause()
      app.globalData.g_currentMusicPostId = null
    }
    //data{}里面的数据只能通过this.setData({})方法改变
    this.setData({
      isPlayMusic
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //监听音乐播放
    backgroundAudioManager.onPlay(() => {
      if (app.globalData.g_currentMusicPostId === this.data.currentPostId) {
        this.setData({
          isPlayMusic: true
        })
      }
      app.globalData.g_isPlayingMusic = true
      // app.globalData.g_currentMusicPostId = this.data.currentPostId
      
    })
    //监听音乐暂停
    backgroundAudioManager.onPause(() => {
      if (app.globalData.g_currentMusicPostId === this.data.currentPostId) {
        this.setData({
          isPlayMusic: false
        })
      }
      app.globalData.g_isPlayingMusic = false
      // app.globalData.g_currentMusicPostId = null
    })
    backgroundAudioManager.onEnded(() => {
      this.setData({
        isPlayMusic: false
      })
      app.globalData.g_isPlayingMusic = false
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})